/*
 * Copyright (c) 2017 Solidwang. All Rights Reserved
 */
package com.solid4j.auth.service.impl;

import com.solid4j.auth.dao.BaseDao;
import com.solid4j.auth.model.Role;
import com.solid4j.auth.page.Page;
import com.solid4j.auth.service.PageService;
import com.solid4j.auth.service.RoleService;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * @author: chengfeng
 * @date: 17/4/17
 */
@Component("roleService")
public class RoleServiceImpl implements RoleService {

    @Resource
    private BaseDao<Role> baseDao;

    @Resource
    private PageService pageService;

    private Page<Role> page;

    @Override
    public Integer save(Role role) {
        return baseDao.save(role);
    }

    @Override
    public Role get(Integer id) {
        return baseDao.get(Role.class, id);
    }

    @Override
    public void delete(Role role) {
        baseDao.delete(role);
    }

    @Override
    public void update(Role role) {
        baseDao.update(role);
    }

    @Override
    public Page<Role> getRoleList(String condition, Integer currentPage, Integer pageSize, String orderBy) {
        if (page == null)
            page = new Page<Role>();
        page.setObjName("Role");
        page.setCondition(condition);
        page.setOrderBy("id desc");
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        page = pageService.queryObjList(page);
        return page;
    }

    @Override
    public List<Role> getRoles() {
        List<Role> list = new ArrayList<Role>();
        try {
            String hql = "from Role";
            list = baseDao.getList(hql);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
